$(function() {
  $('a.batch_add').click(function() {
    data = {}
    data.category_id = $('#add_form select[name=category_id]').val();
    data.tag = $('#add_form input[name=tag]').val();
    data.product = [];
    $('td input.product').each(function() {
      if ($(this).attr('checked') == 'checked') {
      var item = {'pic_url':'', 'num_iid':'', 'coupon_rate':'', 'url':'', 'price':'', 'coupon_price':'', 'coupon_start_time':'', 'coupon_end_time':'', 'mall':'', 'title':''};
      for (k in item) {
        item[k] = $(this).parent().parent().attr('data-' + k.replace(new RegExp('_','gm'), '-'));
      }
      data.product.push(item);
      }
    });
    $.post('/product/add_taobao_product/', data, function(ret){
      if (ret.code == code.succ) {
        alert('操作成功');
        window.location.reload();
      } else {
        alert(ret.msg);
	$('#add_form select[name=category_id]').focus();
      }
    }, 'json');
  });


  $('a.pick').click(function() {
    var that = $(this);
    data = {}
    data.category_id = $('#add_form select[name=category_id]').val();
    data.tag = $('#add_form input[name=tag]').val();
    data.product = [];
    var item = {'pic_url':'', 'num_iid':'', 'coupon_rate':'', 'url':'', 'price':'', 'coupon_price':'', 'coupon_start_time':'', 'coupon_end_time':'', 'mall':'', 'title':''};
    for (k in item) {
      item[k] = $(this).parent().parent().attr('data-' + k.replace(new RegExp('_','gm'), '-'));
    }
    data.product.push(item);
    $.post('/product/add_taobao_product/', data, function(ret){
      if (ret.code == code.succ) {
        //alert('操作成功');
        that.parent().parent().addClass('success');
        that.addClass('hide');
        that.next().removeClass('hide');
        //window.location.reload();
      } else {
        alert(ret.msg);
	$('#add_form select[name=category_id]').focus();
      }
    }, 'json');
  });


  $('a.unpick').click(function() {
    var that = $(this);
    data = {}
    data.category_id = $('#add_form select[name=category_id]').val();
    data.tag = $('#add_form input[name=tag]').val();
    data.product = [];
    var item = {'pic_url':'', 'num_iid':'', 'coupon_rate':'', 'url':'', 'price':'', 'coupon_price':'', 'coupon_start_time':'', 'coupon_end_time':'', 'mall':'', 'title':''};
    for (k in item) {
      item[k] = $(this).parent().parent().attr('data-' + k.replace(new RegExp('_','gm'), '-'));
    }
    data.product.push(item);
    $.post('/product/remove_taobao_product/', data, function(ret){
      if (ret.code == code.succ) {
        //alert('操作成功');
        that.parent().parent().removeClass('success');
        that.addClass('hide');
        that.prev().removeClass('hide');
        //window.location.reload();
      } else {
        alert(ret.msg);
	$('#add_form select[name=category_id]').focus();
      }
    }, 'json');
  });



  $('th input.product').click(function() {
    if ($(this).attr('checked') == 'checked') {
      $('td input.product').attr('checked', 'checked');
    } else {
      $('td input.product').removeAttr('checked');
    }
  });


  $('#set_auto').click(function() {
    var data = {};
    data.category_id = $('#add_form select[name=category_id]').val();
    data.tag = $('#add_form input[name=tag]').val();
    data.mall = 'taobao';
    if (data.category_id == 0) {
      $('#add_form select[name=category_id]').focus();
      alert('请选择商品类型');
      return false;
    }
    data.detail = $(this).attr('data-detail');

    $.post('/product/set_auto/', data, function(ret) {
      if (ret.code == code.succ) {
        alert('操作成功');
      } else {
        alert(ret.msg);
      }
    }, 'json');
  });
});
